﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.UI;
using System.Web.UI.WebControls;

using BabyFood.Entities;
using BabyFood.Data;

public partial class Admin_TinTuc_CNTinTuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpTinTucThumbail + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
            BindCatalog.BindNhomTin(cboNhomTin);
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
            TList<DmSubTinTuc> TList_Catalogs = DataRepository.DmSubTinTucProvider.Find("IdNhomTinTuc=" + cboNhomTin.SelectedValue);
            cboSubTin.DataSource = TList_Catalogs;
            cboSubTin.DataTextField = "TenSubTinTuc";
            cboSubTin.DataValueField = "Id";
            cboSubTin.DataBind();
            HiddenField1.Value = cboSubTin.SelectedValue;
        }
        HiddenField1.Value = cboSubTin.SelectedValue;
    }

    protected void BindData(int IdTinTuc)
    {
        TinTuc entTinTuc = DataRepository.TinTucProvider.GetById(IdTinTuc);
        cboNhomTin.SelectedValue = entTinTuc.IdLoaiTinTuc.ToString();
        cboSubTin.SelectedValue = entTinTuc.IdSubTinTuc.ToString();
        txtTieuDeTin.Text = entTinTuc.TieuDe;
        chkTinNong.Checked = entTinTuc.TinNong;
        H_AnhDaiDien.Value = entTinTuc.AnhDaiDien;
        imgViewer.ImageUrl = entTinTuc.AnhDaiDien.Length > 0 ? Common.sPathAnhDaidien + entTinTuc.AnhDaiDien : "~/Images/no_image.jpg";

        txtTomTat.Text = entTinTuc.TomTat;
        txtContent.Text = Server.HtmlDecode(entTinTuc.NoiDung);
        txtTacGia.Text = entTinTuc.TacGia;
        H_AnhDaiDien.Value = entTinTuc.AnhDaiDien;
    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            TinTuc entTinTuc = new TinTuc();
            entTinTuc.Id = Convert.ToInt32(H_CatalogId.Value);
            entTinTuc.IdLoaiTinTuc = Convert.ToInt32(cboNhomTin.SelectedValue);
            entTinTuc.IdSubTinTuc = Convert.ToInt32(HiddenField1.Value);
            entTinTuc.TieuDe = Common.StandardString(txtTieuDeTin.Text);
            entTinTuc.TomTat = Common.StandardString(txtTomTat.Text);
            entTinTuc.TinNong = chkTinNong.Checked;
            entTinTuc.NoiDung = Server.HtmlEncode(txtContent.Text);
            entTinTuc.TacGia = Common.StandardString(txtTacGia.Text);
            entTinTuc.NgayCapNhat = Common.GetServerDateTime();
            if (H_AnhDaiDien.Value.Length == 0)
            {
                entTinTuc.AnhDaiDien = "~/Images/no_image.jpg";
            }
            else
            {
                entTinTuc.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
            }
            if (txtTacGia.Text == "")
            {
                entTinTuc.TacGia = Common.GetKeyUserInfo("TenTruyCap");
            }
            else
            {
                entTinTuc.TacGia = txtTacGia.Text;
            }
            if (entTinTuc.Id == 0)
            {
                updateStatus = DataRepository.TinTucProvider.Insert(entTinTuc);
                cmdNhapTiep_Click(sender, e);
            }
            else
            {
                entTinTuc.EntityState = EntityState.Changed;
                updateStatus = DataRepository.TinTucProvider.Update(entTinTuc);
                cmdNhapTiep_Click(sender, e);
            }
            if (!updateStatus)
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.TINTUC_CAPNHAT_THANHCONG, true);
                H_CatalogId.Value = entTinTuc.Id.ToString();
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("CNTinTuc.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.TINTUC_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        H_FileName.Value = "";
        imgViewer.ImageUrl = "~/Images/no_image.jpg";
        H_AnhDaiDien.Value = "~/Images/no_image.jpg";
        cboNhomTin.SelectedValue = "0";
        txtTieuDeTin.Text = "";
        txtTomTat.Text = "";
        chkTinNong.Checked = true;
        txtContent.Text = "";
        txtTacGia.Text = "";
    }

    protected void SelectedindexchangedcboNhomTin(object sender, EventArgs e)
    {
        TList<DmSubTinTuc> TList_Catalogs = DataRepository.DmSubTinTucProvider.Find("IdNhomTinTuc=" + cboNhomTin.SelectedValue);
        cboSubTin.DataSource = TList_Catalogs;
        cboSubTin.DataTextField = "TenSubTinTuc";
        cboSubTin.DataValueField = "Id";
        cboSubTin.DataBind();
    }

    protected void SelectedindexchangedcboSubTin(object sender, EventArgs e)
    {
        HiddenField1.Value = cboSubTin.SelectedValue;
    }
    protected void cmdUpload_Click(object sender, EventArgs e)
    {
    }
}
